using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Text.RegularExpressions;

using DailyTestBLL.XMLParse;
using DailyTestBLL.PoolTest;
using DailyTestBLL.UserDefineAttributes;

using Winista.Text.HtmlParser.Lex;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Extractors;
using Winista.Text.HtmlParser.Data;
using Winista.Text.HtmlParser.Filters;
using Winista.Text.HtmlParser.Http;

namespace DailyTestConsole
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //XmlSchemaDisplay.StartUp();
            //UseMsSchema();
            //ClientUse();
            //SaySomething();
            //ThreadTest1();
            //i changed somthing
            //Console.WriteLine(DateTime.Now.ToString());
            //MTestEquityXOILogin(1);
            //Console.WriteLine(DateTime.Now.ToString());
            //MTestEquityXOILogin(2);
            //Console.WriteLine(DateTime.Now.ToString());
            //Console.Read();
            //TCPHttp tcp = new TCPHttp();
            //string str = tcp.GetResponse("GET");
            //Console.WriteLine(str);
            //str = tcp.GetResponse("GET");
            //Console.WriteLine(str);

            //Regex reg = new Regex(@"(^(((?<day>\d+)(d|D))?((?<hour>\d+)(h|H))?((?<minute>\d+)(m|M))?((?<second>\d+)(s|S))?)$)|(^(?<hour>\d+)+(:{1}(?<minute>\d+))?(:{1}(?<second>\d+))?$)");
            //string str = "12m";
            //Console.WriteLine( "day:" + reg.Match(str).Groups["day"].ToString());
            //Console.WriteLine( "hour:" + reg.Match(str).Groups["hour"].ToString());
            //Console.WriteLine("minute:" + reg.Match(str).Groups["minute"].ToString());
            //Console.WriteLine( "second:" + reg.Match(str).Groups["second"].ToString());
            //str = "23";
            //Console.WriteLine("day:" + reg.Match(str).Groups["day"].ToString());
            //Console.WriteLine("hour:" + reg.Match(str).Groups["hour"].ToString());
            //Console.WriteLine("minute:" + reg.Match(str).Groups["minute"].ToString());
            //Console.WriteLine("second:" + reg.Match(str).Groups["second"].ToString());

            //Console.WriteLine(typeof(int[][,]));

            //Type[] types = new Type[] { typeof(int),
            //                            typeof(int[]),
            //                            typeof(int[][]),
            //                            typeof(int[,]),
            //                            typeof(int[,][,]),
            //                            typeof(int[][,]),
            //                            typeof(int[,][]),
            //                            typeof(int[,,,,,][,,,,][,,,]) };

            //foreach (Type t in types)
            //{
            //    Console.WriteLine("Type.ToString() = {0}", t.ToString());
            //    Console.WriteLine("GetTypeString(Type) = {0}", GetTypeString(t));

            //    CodeTypeReference codeTypeReference = new CodeTypeReference(t);
            //    Console.WriteLine("GetTypeString(CodeTypeReference) = {0}", GetTypeString(codeTypeReference));
            //}

            //CountChar.test();

            //Console.Read();

            GetResult();
            Console.Read();
        }

        private static void GetResult()
        {
            string strHtml = "<div class='divCss' id='div_1'>"+
                "<div name='div' class='divCss' id='div_2'>div_2</div>" +
                "<table name='table' id='table_1'>"+
                "<tr>"+
                "<td>HtmlParser</td>"+
                "<td><div id='div_3'><font color='red'>HtmlParser</font></div></td>"+
                "</tr>"+
                "</table>"+
                "</div>";
            Lexer lex = new Lexer(strHtml);
            Parser parse = new Parser(lex);
            NodeFilter filter = new NodeClassFilter(typeof(Div));
            NodeList nodelist = parse.Parse(null);
            if (nodelist.Count == 0)
                Console.WriteLine("no content");
            else
            {
                for (int i = 0; i < nodelist.Count; i++)
                {
                    parseData(nodelist[i]);
                }
            }
        }

        private static ITag getTag(INode node)
        {
            if (node == null)
                return null;
            return node is ITag ? node as ITag : null;
        }

        static IList<int> start = new List<int>();

        private static void parseData(INode node)
        {
            ITag tag = getTag(node);
            if (tag != null && !tag.IsEndTag() && !start.Contains(tag.StartPosition))
            {
                object oId = tag.GetAttribute("ID");
                object oName = tag.GetAttribute("name");
                object oClass = tag.GetAttribute("class");
                Console.WriteLine(string.Format("{0}:\r\nID:{1}\r\nName:{2}\r\nClass:{3}\r\nContent:{5}\r\nStartPosition:{4}\r\n",tag.TagName,oId,oName,oClass,tag.StartPosition,tag.ToPlainTextString()));
                start.Add(tag.StartPosition);
            }
            if (node.Children != null && node.Children.Count > 0)
            {
                parseData(node.FirstChild);
            }
            INode siblingNode = node.NextSibling;
            while (siblingNode != null)
            {
                parseData(siblingNode);
                siblingNode = siblingNode.NextSibling;
            }
        }

        private static string GetTypeString(Type type)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (!type.IsArray)
                return type.BaseType.ToString();

            switch (type.GetArrayRank())
            {
                case 0:
                    throw new ArgumentOutOfRangeException("type.GetArrayRank()");

                case 1:
                    return string.Format("{0}[]", GetTypeString(type.GetElementType()));

                default:
                    return string.Format("{0}[{1}]", GetTypeString(type.GetElementType()), new string(',', type.GetArrayRank() - 1));
            }
        }

        private static string GetTypeString(CodeTypeReference typeRef)
        {
            if (typeRef == null)
                throw new ArgumentNullException("typeRef");

            switch (typeRef.ArrayRank)
            {
                case 0:
                    return typeRef.BaseType;

                case 1:
                    return string.Format("{0}[]", GetTypeString(typeRef.ArrayElementType));

                default:
                    return string.Format("{0}[{1}]", GetTypeString(typeRef.ArrayElementType), new string(',', typeRef.ArrayRank - 1));
            }
        }

        static void MTestEquityXOILogin(int i)
        {
            TestEquityXOILogin test = new TestEquityXOILogin();
            test.GetUrl(i);
        }

        //static void ThreadTest1()
        //{
        //    ThreadTest tt = new ThreadTest();
        //    tt.Test();
        //    //tt.AfterA += new AfterADelegate(tt_AfterA);
        //    tt.A();
        //    Console.WriteLine(\"AfterMain\");
        //}

        //static void tt_AfterA()
        //{
        //    Console.WriteLine(\"tt_AfterA\");
        //    ThreadStart tsAftera = new ThreadStart(SomeA);
        //    Thread thAfterA = new Thread(tsAftera);
        //    thAfterA.Start();
        //    thAfterA.Join();
        //    Console.WriteLine(\"tt_AfterAJoin\");
        //}

        //static void SomeA()
        //{
        //    Thread.Sleep(2000);
        //    Console.WriteLine(\"AfterAThread\");
        //}

        //static void SaySomething()
        //{
        //    TestClass tc = new TestClass();
        //    Assembly asm = Assembly.LoadFrom(\"DailyTest.dll\");
        //    Type t = asm.GetType(\"DailyTestBLL.UserDefineAttributes.TestClass\");
        //    //Type t = tc.GetType();
        //    object[] objs = t.GetCustomAttributes(false);
        //    foreach (object obj in objs)
        //    {
        //        if (obj is ModuleName)
        //        {
        //            ModuleName mn = obj as ModuleName;
        //            Console.WriteLine(mn.Name);
        //        }
        //    }
        //}

        //static private void ClientUse()
        //{
        //    AClientClass a = new AClientClass();
        //    ThreadPoolTest tp = new ThreadPoolTest(a,10);
        //    for (int i = 0; i < 8; i++)
        //    {
        //        tp.DoWork(2);
        //    }
        //}

        //static private void TestMayParse()
        //{
        //    FileStream fs = File.Open(\"D:\\ShortInterest.xsd\", FileMode.Open);
        //    byte[] buff = new byte[1024000];
        //    int iRead = fs.Read(buff, 0, buff.Length);
        //    string strContext = System.Text.ASCIIEncoding.ASCII.GetString(buff, 0, iRead);
        //    XMLParse xmlP = new XMLParse(strContext);
        //    xmlP.Prase();
        //}

        //static private void UseMsSchema()
        //{
        //    XmlSchemaSet schemaSet = new XmlSchemaSet();
        //    DirectoryInfo dicInfo = new DirectoryInfo(\"D:\\EquityDataSchema\");
        //    FileInfo[] files = dicInfo.GetFiles();
        //    foreach (FileInfo fi in files)
        //    {
        //        if (fi.Extension == \".xsd\")
        //        {
        //            FileStream fs = fi.OpenRead();
        //            XmlSchema tmpSchema = XmlSchema.Read(fs,new ValidationEventHandler(Valid) );
        //            schemaSet.Add(tmpSchema);
        //        }
        //    }
        //    schemaSet.Compile();
        //    IDictionaryEnumerator dicenu = schemaSet.GlobalElements.GetEnumerator();
        //    ICollection col = schemaSet.Schemas();
        //    //XmlNameTable xmltable = schemaSet.NameTable;
        //    IEnumerator enu = col.GetEnumerator();
        //    while (enu.MoveNext())
        //    {
        //        XmlSchema schema = enu.Current as XmlSchema;
        //        StringBuilder sbOutput = new StringBuilder();
        //        XmlWriter xmlW = XmlWriter.Create(sbOutput);
        //        schema.Write(xmlW);

        //        Console.WriteLine(string.Format(\"Id:{0}\", schema.Id));
        //    }

        //    //while (enu.MoveNext())
        //    {
        //        //string strText = \"Name:{0},\r\nValue:{1}\r\n\r\n\";
        //        //XmlSchemaElement schemaEle = enu.Value as XmlSchemaElement;
                
        //        //Console.WriteLine(enu.Value.GetType());
        //        //Console.WriteLine(string.Format(strText, enu.Key, enu.Value));
        //    }
        //}

        //static void Valid(object target, ValidationEventArgs e)
        //{
        //    Console.WriteLine(e.Exception.Message);
        //}
    }
}
